/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.uloha3.swing;

import cz.muni.fi.pv168.uloha3.entities.Owner;
import cz.muni.fi.pv168.uloha3.entities.RealEstate;
import cz.muni.fi.pv168.uloha3.swing.workers.OwnerListAllSwingWorker;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author morty
 */
public class OwnersTableModel extends AbstractTableModel{
    
    private List<Owner> ownersList = new ArrayList<Owner>();
    private Locale localization = Locale.getDefault();
    //private Locale localization = new Locale("cs", "CZ");
    
    
    public OwnersTableModel() {
        OwnerListAllSwingWorker worker = new OwnerListAllSwingWorker(this);
        worker.execute();
    }
    
    public OwnersTableModel(RealEstate excludeForRealEstate) {
        OwnerListAllSwingWorker worker = new OwnerListAllSwingWorker(excludeForRealEstate, this);
        worker.execute();
    }
    
    @Override
    public int getRowCount() {
        return ownersList.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Owner owner = ownersList.get(rowIndex);
        switch(columnIndex) {
            case 0:
                return owner.getFirstName();
            case 1:
                return owner.getLastName();
            case 2:
                return owner.getSsn();
            case 3:
                return owner.getAddress();
            case 4:
                return owner.getTelephone();
            case 5:
                return owner.getId().toString();
            default:
                throw new IllegalArgumentException(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnersTableModel", localization).getString("COLUMNINDEX"));
        }
    }
    
    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnersTableModel", localization).getString("NAME");
            case 1:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnersTableModel", localization).getString("SURNAME");
            case 2:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnersTableModel", localization).getString("SSN");
            case 3:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnersTableModel", localization).getString("ADDRESS");
            case 4:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnersTableModel", localization).getString("TELEPHONE");
            case 5:
                return java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnersTableModel", localization).getString("ID");
            default:
                throw new IllegalStateException(java.util.ResourceBundle.getBundle("cz/muni/fi/pv168/uloha3/swing/properties/OwnersTableModel", localization).getString("COLUMNINDEX"));
        }
    }
    
    public void addOwner(Owner owner) {
        ownersList.add(owner);
        int lastRow = ownersList.size()-1;
        fireTableRowsInserted(lastRow, lastRow);
    }
    
    public void removeOwner(Owner owner) {
        int rownum = ownersList.indexOf(owner);
        ownersList.remove(rownum);
        fireTableRowsDeleted(rownum, rownum);
    }
    
    public void addAllOwners(Set<Owner> ownerList) {
        ownersList.addAll(ownerList);
        fireTableDataChanged();
    }
    
    public void clearAllOwners() {
        ownersList.clear();
        fireTableDataChanged();
    }
    
    public void updateOwner(Owner owner) {
        ownersList.set(ownersList.indexOf(owner), owner);
        fireTableRowsUpdated(ownersList.indexOf(owner), ownersList.indexOf(owner));
    }
    
    public Owner getOwnerAt(int rownum) {
        return ownersList.get(rownum);
    }
}
